﻿using JamesZhao.Engine.Graphics.Effects;
using JamesZhao.Engine.Graphics.Surfaces;
using SlimDX;
using SlimDX.Direct3D11;
using SlimDX.DXGI;

namespace JamesZhao.Engine.Graphics.RenderStages
{
    internal class Hdr2LdrStage : PostProcessStage
    {
        private EffectResourceVariable _varHdrTexture;
        private EffectVectorVariable _varInputSize;


        public Hdr2LdrStage(GraphicsDevice device, ColorSurface inputSurface, ColorSurface[] scratchSurfaces)
            : base(inputSurface, scratchSurfaces)
        {
            Hdr2Ldr = new GpuProgram(device, "Shaders/Hdr2Ldr.fx");
            CreateFullScreenQuad(device, Hdr2Ldr);
        }

        public GpuProgram Hdr2Ldr { get; set; }

        public override string Name
        {
            get { return "Hdr to Ldr"; }
        }

        protected override void OnRender(GraphicsDevice device, SwapChain swapChain, VisualsList visualsList,
                                         Camera camera)
        {
            if (_varHdrTexture == null)
            {
                _varHdrTexture = Hdr2Ldr.ResourceVariables["hdrTexture"];
                _varInputSize = Hdr2Ldr.VectorVariables["inputSize"];
            }


            _varHdrTexture.SetResource(InputScratchSurface.ResourceView);
            _varInputSize.Set(new Vector2(InputScratchSurface.ActualSize.Width, InputScratchSurface.ActualSize.Height));

            device.ImmediateContext.OutputMerger.SetTargets(OutputScratchSurface.TargetView);
            FullScreenQuad.RenderWithGpuProgram(device, Hdr2Ldr);
        }
    }
}